﻿/**
 * ...
 * @author Default
 * @version 0.1
 */

package com.crlclr.ui {
	import caurina.transitions.Tweener;
	
	import com.crl.core.ICommand;
	import com.crl.core.IResizeable;
	import com.crl.managers.ResizeableManager;
	import com.crl.ui.controls.Component;
	import com.crlclr.data.AlbumData;
	import com.crlclr.events.BookEvent;
	import com.crlclr.managers.*;
	import com.crlclr.utils.Padding;
	
	import flash.display.*;
	import flash.events.*;
	import flash.text.TextField;
	
	public class QFootBar extends Component implements ICommand ,IResizeable{
		private static const LINKAGE:String = "BarBg";
		
		
		private var padding:Padding;
		private var margin:Padding;
		
		
		private var padL:int;
		private var padR:int;
		
		private var bg:*;
		private var infoIcon:Bitmap;
		private var thumbContainer:Sprite;
		private var selectThumb:QThumb;
		private var stateTextFiled:TextField;
		
		private var thumbList:Array;
		
		private var showThumbState:Boolean;
		private var _data:AlbumData;
		private var a:int = 0;
		
		//辅助功能;
		
		public function QFootBar() {
			super();
		}
		
		override protected function defaults() : void{
			thumbList=new Array();
			
		}
		
		protected override function create():void {
			var skin:Class = SkinManager.getInstance().getClass(LINKAGE);
			var icon:Class = SkinManager.getInstance().getClass("InfoBmd");
			
			
			bg = new skin();
			addChild(bg);
			
			infoIcon = new Bitmap(new icon(0, 0) as BitmapData);
			infoIcon.x = 5;
			addChild(infoIcon);
			
			stateTextFiled = new TextField();
			stateTextFiled.x = 10+infoIcon.width;
			
			QTextFormat.bindDefaultFiled(stateTextFiled, QTextFormat.statusBarTextFormat());
			addChild(stateTextFiled);
			
			
			thumbContainer = new Sprite();
			addChildAt(thumbContainer,0);
		}
		
		override protected  function init():void {
			padding = new Padding(80,80);
			margin = new Padding(3,3);
			padL = bg.left.width;
			padR = bg.right.width;
			
			ResizeableManager.getInstance().add(this);
		}
		
		
		
		public function set data(value:AlbumData):void {
			this._data = value as AlbumData;
			this.execute();
		}
		public function close():void {
			if(thumbContainer.parent){
				thumbContainer.parent.removeChild(thumbContainer);
			}
			removeEvent();
			stateTextFiled.text = "";
		}
		
		
		public function execute(e:*=null):void{
			if(thumbContainer.parent==null){
				this.addChildAt(thumbContainer,0);
			}
			this.createThumbs();
			
			showThumbState = false;
			thumbContainer.y=0;
			Tweener.addTween(thumbContainer, { y:padding.padY + margin.padY, time:1, delay:3, onComplete:addEvent} );
		}
		
		private function addEvent():void {
			this.addEventListener(Event.ENTER_FRAME, enterHander);
			stage.addEventListener(Event.MOUSE_LEAVE,activateHandler);
			stage.addEventListener(MouseEvent.MOUSE_OVER,activateHandler);
		}
		
		private function activateHandler(event:Event):void{
			if(event.type==Event.MOUSE_LEAVE){
				this.removeEventListener(Event.ENTER_FRAME, enterHander);
			}else{
				if(this.hasEventListener(Event.ENTER_FRAME)==false)this.addEventListener(Event.ENTER_FRAME, enterHander);
			}
		}
		
		private function removeEvent():void {
			this.removeEventListener(Event.ENTER_FRAME, enterHander);
			stage.removeEventListener(Event.MOUSE_LEAVE,activateHandler);
			stage.removeEventListener(Event.ACTIVATE,activateHandler);
		}
		
		
		private function createThumbs():void{
			var len:int = _data.length;
			
			while(thumbContainer.numChildren){
				thumbContainer.removeChildAt(0);
			}
			
			
			if(len==0)return;
			
			var buffer:Array=thumbList.concat();
			thumbList.splice(0);
			
			
			var thumb:QThumb;
			for (var i:int = 0; i < len; i++) {
				if(buffer.length>0){
					thumb=buffer.pop();
				}else{
					thumb= new QThumb();
				}
				thumb.index=i;
				thumb.data = this._data.getPageData(i);
				
				thumb.x = (i) * (padding.padX) + i * margin.padX;
				thumb.addEventListener(MouseEvent.MOUSE_OVER, mouserHander);
				thumb.addEventListener(MouseEvent.MOUSE_OUT, mouserHander);
				thumb.addEventListener(BookEvent.THUMBCHANGET, thumbChange);
				
				thumbList.push(thumb);
				thumbContainer.addChild(thumb);
			}
			
			this.resize(stage.stageWidth, stage.stageHeight);
		}
		
		private function mouserHander(event:MouseEvent):void {
			if (event.type == MouseEvent.MOUSE_OVER) {
				stateTextFiled.text ="打开:"+(event.currentTarget as QThumb).data.desc;
				return;
			}
			stateTextFiled.text =selectThumb.data.desc;
		}
		
		
		private function thumbChange(event:BookEvent):void {
			dispatchEvent(event.Qclone());
		}
		
		public function getThumb(index:int):QThumb {
			return this.thumbList[index] as QThumb;
		}
		override public  function resize(w:int,h:int):void{
			if (thumbContainer == null) return;
			
			//bg  height 20;
			var bgHeight:int = 20;
			var bgCenterWidth:int = w - (padL + padR) + 12;
			var bgRightX:int=bg.center.x+bgCenterWidth;
			this.y = h - (padding.padY + bgHeight);
			
			bg.center.width =bgCenterWidth ;
			bg.right.x = bgRightX;
			
			bg.y = padding.padY;
			infoIcon.y = padding.padY + 2;
			stateTextFiled.y = padding.padY;
			
			thumbContainer.x = 0;
			
			
			if (this.thumbContainer.width < w ) {
				thumbContainer.x = (w - thumbContainer.width) / 2;
			}
		}
		private function enterHander(event:Event):void {
			var mx:int = stage.mouseX;
			var my:int = stage.mouseY;
			var w:int = stage.stageWidth;
			var h:int = stage.stageHeight;
			
			if (my > h - padding.padY-20) {
				if (!showThumbState) {
					showThumbState = true;
					Tweener.addTween(thumbContainer, { y:0, time:1} );
					return;
				}
			}else {
				if (showThumbState) {
					showThumbState = false;
					Tweener.addTween(thumbContainer, { y:padding.padY, time:1,onComplete:this.center } );
					return;
				}
				return;
			}
			
			
			//内容太少时.或者还未全部出现时;
			if (this.thumbContainer.width < w ||thumbContainer.y-2>0) {
				return;
			}
			
			if (this.hitTestPoint(mx, my)) {
				
				if (mx>w-100) {
					a=-15;	
				} else if (mx<100) {
					a=15;
				} else {
					a=0;
				}
			}else {
				a = 0;
			}
			
			
			
			thumbContainer.x += a;
			if (thumbContainer.x > 0) {
				thumbContainer.x = 0;
			}
			if(thumbContainer.x<w-thumbContainer.width){
				thumbContainer.x = w - thumbContainer.width;
			}
		}
		
		public function pageChange(event:BookEvent):void {
			if (selectThumb != null) {
				selectThumb.unselect();
			}
			selectThumb = getThumb(event.page.index);
			selectThumb.select();
			center(event.page.index);
			
			stateTextFiled.text =selectThumb.data.desc;
		}
		
		
		public function center(index:int = -1):void {
			if (index == -1) {
				index=selectThumb.index;
			}
			
			var w:int = stage.stageWidth;
			var tw:int = this.thumbContainer.width;
			if ( tw < w ) {
				return;
			}
			
			var newX:int = (index) * (padding.padX) + index * margin.padX - (w - padding.padX) / 2;
			newX = -newX;
			
			if (newX > 0) {
				newX = 0;
			}else if(newX+tw<w) {
				newX = w - tw;
			}
			Tweener.addTween(thumbContainer, { x:newX, time:1, delay:0} );
		}
	}
	
}
